package com.huazheng.common.result;

import lombok.Data;

import java.util.HashMap;
import java.util.Map;

/**
 * @Classname R
 * @Description 交易结果类
 * @Date 2020/5/25 15:17
 * @Created by zhanghuazheng
 */

@Data
public class R {

    private Boolean success;

    private Integer code;

    private String message;

    private Map<String, Object> data = new HashMap<>();

    // 构造器私有
    public R(){}

    // 通用返回成功
    public static R ok() {
        R r = new R();
        r.setSuccess(ResultCodeEnum.SUCCESS.isSuccess());
        r.setCode(ResultCodeEnum.SUCCESS.getCode());
        r.setMessage(ResultCodeEnum.SUCCESS.getMsg());
        return r;
    }

    // 通用返回失败，未知错误
    public static R fail() {
        R r = new R();
        r.setSuccess(ResultCodeEnum.FAIL.isSuccess());
        r.setCode(ResultCodeEnum.FAIL.getCode());
        r.setMessage(ResultCodeEnum.FAIL.getMsg());
        return r;
    }

    // 设置结果，形参为结果枚举
    public static R setResult(ResultCodeEnum result){
        R r = new R();
        r.setSuccess(result.isSuccess());
        r.setCode(result.getCode());
        r.setMessage(result.getMsg());
        return r;
    }

    /**
     * 使用链式编程
     * @return
     */
    public  R data(Map map){
        this.setData(map);
        return this;
    }

    public  R data(String key,Object value){
        this.data.put(key, value);
        return this;
    }

    public  R code(Integer code){
        this.setCode(code);
        return this;
    }

    public  R msg(String  msg){
        this.setMessage(msg);
        return this;
    }


}
